package com.example.android.fragments

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import com.example.android.R
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import com.example.android.extensions.Services
import com.example.android.models.User
import com.example.android.viewmodels.SelectBedViewModel
import com.example.android.viewmodels.SharedViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


/**
 * @ClassName SelectBedFragment
 * @Author Lucas
 * @Date 2022/6/3
 * @Description
 **/
class SelectBedFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    private val _Model : SharedViewModel by activityViewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_select_bed, container, false)

    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        lifecycleScope.launch(Dispatchers.Main) {
            Services.SharedService.GetSelfInfo(_Model)
        }

        //val model: SelectBedViewModel by viewModels()
        _Model.getSelf().observe(this, Observer<User>{ user ->
            // update UI
            Log.d(toString(), "uuuuuuuuu")
            val name = view.findViewById<TextView>(R.id.stu_name)
            name.text = user.name
            val id_number = view.findViewById<TextView>(R.id.stu_id_number)
            id_number.text = user.identity
            val college = view.findViewById<TextView>(R.id.stu_college)
            college.text = user.college
            val stu_no = view.findViewById<TextView>(R.id.stu_no)
            stu_no.text = user.userName
            val cclass = view.findViewById<TextView>(R.id.stu_class)
            cclass.text = user.classes


        })

//        revise.setOnClickListener {
//
//           // Toast.makeText(this, "Android Short Toasts", Toast.LENGTH_SHORT).show()
//            AlertDialog {
//                it.SetContent("abc")
//            }
//
////            val builder = AlertDialog.Builder(revise.context)
////            //val builder: AlertDialog.Builder = AlertDialog.Builder(this)
////            builder.setTitle("尊敬的用户")
////                   .setMessage("你真的要卸载我吗？")
////
////                .create()
////                .show()
//
//
//        }



        val next_button = view.findViewById<Button>(R.id.next)
        next_button.setOnClickListener {
            val select_bed_step2 = SelectBed2Fragment()

            requireActivity().supportFragmentManager
                .beginTransaction()
                .setCustomAnimations(
                    R.anim.fragment_item_select_in,
                    R.anim.fragment_item_select_out,
                    R.anim.fragment_item_back_in,
                    R.anim.fragment_item_back_out)
                .replace(R.id.content_main_fragment,select_bed_step2)
                .addToBackStack(null)
                .commit()
        }






    }


}